/**
 * 牌桌聊天窗口
 */
var ChatWindow = Window.extend({
    _ccsFileName:chat.res.ChatWindow_json,
    _notCloseBlock: false,
    /**
     * 初始化界面
     * @private
     */
    initWindow: function () {
        var self = this;
        var key = findKey(app.map_id, gameData.mapId);
        var sub = null;
        if (key) {
            sub = app.map_id_2_sub[key];
        }
        self.isMajiang = (sub && sub == app.sub_module.MaJiang_hn);
        self._nodeList["root.majiang"].setVisible(false);
        self._nodeList["root.poker"].setVisible(false);
        var biaoqingClick = function () {
            self._nodeList["root.btn_biaoqing.btn_focus"].setVisible(true);
            self._nodeList["root.btn_changyongyu.btn_focus"].setVisible(false);
            self._nodeList["root.caishen"].setVisible(true);
            self._nodeList["root.changyongyu"].setVisible(false);
        };
        var changyongyuClick = function () {
            self._nodeList["root.btn_biaoqing.btn_focus"].setVisible(false);
            self._nodeList["root.btn_changyongyu.btn_focus"].setVisible(true);
            self._nodeList["root.caishen"].setVisible(false);
            self._nodeList["root.changyongyu"].setVisible(true);
        };
        TouchUtil.setOnclickListener(self._nodeList["root.btn_biaoqing"], biaoqingClick, {sound: "tab"});
        TouchUtil.setOnclickListener(self._nodeList["root.btn_changyongyu"], changyongyuClick, {sound: "tab"});
        changyongyuClick();
        self._initText();
        self._initInput();
        self._initExpression();
    },

    /**
     * 初始化 文本
     */
    _initText: function () {
        var self = this;
        self.isFangyan = settingData.local_dialect[gameData.mapId];
        self.wordsArr = [
            '快点出牌了'
            , '今天手气还可以'
            , '上碰下自摸'
            , '辛辛苦苦十多年，一把回到解放前'
            , '抓的个破牌'
            , '你屋里钱多'
            , '今天好开心'
            , '莫走，莫走，大家通宵'
            , '又掉线了，网络太差了吧'
            , '不好意思，我有事先走了'
        ];
        if (self.isFangyan && gameData.mapId == app.map_id.QYLDS){
            self.wordsArr = [
                '快点出牌了'
                , '今天手气还可以'
                , '上碰下自摸'
                , '辛辛苦苦十多年，一把回到解放前'
                , '抓的个破牌'
                , '你屋里钱多'
                , '今天好开心'
                , '莫走，莫走，大家通宵'
                , '又掉线了，网络太差了吧'
                , '不好意思，我有事先走了'
            ];
            self.soundArr = [
                ['快点出牌了'],
                ['今天手气还可以'],
                ['上碰下自摸'],
                ['辛辛苦苦十多年，一把回到解放前'],
                ['抓的个破牌'],
                ['你屋里钱多'],
                ['今天好开心'],
                ['莫走，莫走，大家通宵'],
                ['又掉线了，网络太差了吧'],
                ['不好意思，我有事先走了'],
            ];
        }
        else  if (self.isFangyan && gameData.mapId == app.map_id.YZCHZ) {
            self.wordsArr = [
                '那么慢，快点出牌啦'
                , '今天手气蛮好的'
                , '这个牌吃的好喽'
                , '打牌不行，全靠走心'
                , '不好意思，我有事先走了'
                , '打这个牌，你屋里钱多'
                , '你也太狠了嘛'
                , '又掉线了，网络真差'
                , '今天蛮开心的'
            ];
            self.soundArr = [
                ['那么慢，快点出牌啦'],
                ['今天手气蛮好的'],
                ['这个牌吃的好喽'],
                ['打牌不行，全靠走心'],
                ['不好意思，我有事先走了'],
                ['打这个牌，你屋里钱多'],
                ['你也太狠了嘛'],
                ['又掉线了，网络真差'],
                ['今天蛮开心的'],
            ];
        }
        else if (self.isFangyan) {
            self.wordsArr = [
                '今天真的背'
                , '别墨迹了'
                , '哈哈,手气正好'
                , '气死我了'
                , '叫我怎么说呢'
                , '你太牛了'
            ];
            self.soundArr = [
                [
                    '今天真的背',
                    '我的个娘啊',
                    '下手别这样重啊',
                    '想哭却哭不出来'
                ],
                [
                    '别墨迹了',
                    '快点快点,别打酱油了',
                    '想啥呢?出牌啊',
                    '咋比乌龟还慢呢'
                ],
                [
                    '哈哈,手气正好',
                    '今儿真高兴',
                    '爽爆了',
                    '我可以不笑么?哈哈哈哈'
                ],
                [
                    '放学你别走',
                    '气死我了',
                    '伤不起',
                    '咋这么菜呢'
                ],
                [
                    '汗汗汗汗',
                    '叫我怎么说呢',
                    '你不懂我的心啊',
                    '无语'

                ],
                [
                    '必须点个赞',
                    '你太牛了',
                    '神一样的队友',
                    '这牌打得好'
                ]

            ];
        }
        var scrollView = this._nodeList["root.changyongyu.ScrollView"];
        var row = this._nodeList["root.changyongyu.ScrollView.row"];
        var children = scrollView.getChildren();
        for (var i = 0; i < children.length; i++) {
            children[i].setVisible(false);
        }
        var tmpHeight = self.wordsArr.length * (row.getContentSize().height + 10);
        var innerHeight = Math.max(scrollView.getContentSize().height,tmpHeight);
        for (var i = 0; i < self.wordsArr.length; i++) {
            var node = this._nodeList["root.changyongyu.ScrollView.row" + i];
            if (!node) {
                node = duplicateNode(row);
                node.setVisible(true);
                node.setName('row' + i);
                row.getParent().addChild(node);
                this._nodeList["root.changyongyu.ScrollView.row" + i] = node;
            }
            node.setPositionY(innerHeight - (row.getContentSize().height + 10) * (i + 1/2));
            var label = node.getChildByName("text");
            label.setString(self.wordsArr[i]);
            label.setLocalZOrder(1);
            self._setWordsClickCallback(node, i);
        }
        scrollView.innerHeight = innerHeight;
    },
    /**
     * 文字点击回调
     */
    _setWordsClickCallback: function (node, i) {
        var self = this;
        TouchUtil.setOnclickListener(node, function () {
            var sex = ((gameData.sex == null || gameData.sex == 0) ? '1' : gameData.sex);
            if(self.isFangyan){
                var _soundArr = self.soundArr[i];
                var rand = Math.floor(Math.random() * _soundArr.length);
                var text = 'vcsv' + (i + 1) + '_' + rand + '_' + sex;
                self.sendText(_soundArr[rand], text);
            }else{
                self.sendText(self.wordsArr[i], 'vv' + (i + 1) + '_' + sex);
            }
            self.removeFromParent(false);
        }, {swallowTouches: false, effect: "none"});
    },
    /**
     * 手动输入发送聊天信息
     */
    _initInput: function () {
        var self = this;
        var input = self._nodeList["root.input"];
        input.setTextVerticalAlignment(cc.VERTICAL_TEXT_ALIGNMENT_CENTER);
        TouchUtil.setOnclickListener(self._nodeList["root.btn_send"], function () {
            input.didNotSelectSelf();
            var str = _.trim(input.getString());
            if (!!str) {
                self.sendText(input.getString());
                input.setString("");
                self.removeFromParent(false);
            }
        });
    },
    /**
     * 初始化 表情动画
     */
    _initExpression: function () {
        var self = this;
        for (var i = 1; i <= 7; i++) {
            var node = this._nodeList['root.majiang.biaoqing' + i];
            self._setEmojiClickCallback(node, i);
        }
        for (var i = 1; i <= 9; i++) {
            var node = this._nodeList['root.poker.biaoqing' + i];
            self._setEmojiClickCallback(node, i);
        }
        for (var i = 1; i <= 12; i++) {
            var node = this._nodeList['root.caishen.biaoqing' + i];
            self._setEmojiClickCallback(node, i);
        }
    },
    /**
     * 表情回调
     */
    _setEmojiClickCallback: function (node, i) {
        var self = this;
        TouchUtil.setOnclickListener(node, function () {
            // if(self.isMajiang){
            //     self.sendBiaoQing(EmojiUtil.emojiType.kwx_majiang, i);
            // }else{
            //     self.sendBiaoQing(EmojiUtil.emojiType.pdk, i);
            // }
            self.sendBiaoQing(EmojiUtil.emojiType.caishen, i);
            self.removeFromParent(false);
        }, {swallowTouches: false});
    },
    /**
     * 发送表情
     * @param emojiType
     * @param content
     */
    sendBiaoQing: function (emojiType, content) {
        if (gameData.wanfatype == 'java') {
            network.send(3008, {room_id: gameData.roomId, type: emojiType, content: content});
        }else if(gameData.wanfatype == 'go'){
            var msg = JSON.stringify({
                roomid: gameData.roomId,
                type: emojiType,
                voice: '',
                content: content,
                from: gameData.uid,
                uid: gameData.uid
            });
            network.sendPhz(5000, "Say/" + msg);
        }else{
            if (gameData.subModule == SUB_MODULE.NN) {
                var msg = JSON.stringify({
                    roomid: gameData.roomId,
                    type: emojiType,
                    voice: '',
                    content: content,
                    uid: gameData.uid
                });
                network.wsData("Say/" + msg);
            } else if (gameData.subModule == SUB_MODULE.HuZi) {
                var msg = JSON.stringify({
                    roomid: gameData.roomId,
                    type: emojiType,
                    voice: '',
                    content: content,
                    from: gameData.uid
                });
                network.sendPhz(5000, "Say/" + msg);
            }
        }
    },
    /**
     * 发送文本
     * @param {String} voice
     * @param {String} content
     */
    sendText: function (content, voice) {
        voice = voice || '';
        if (gameData.wanfatype == 'java') {
            network.send(3008, {room_id: gameData.roomId, voice: voice, type: 'text', content: content});
        }else if(gameData.wanfatype == 'go'){
            var msg = JSON.stringify({
                roomid: gameData.roomId,
                type: 'text',
                voice: voice,
                content: content,
                uid: gameData.uid
            });
            network.sendPhz(5000, "Say/" + msg);
        }else{
            if (gameData.subModule == SUB_MODULE.NN) {
                var msg = JSON.stringify({
                    roomid: gameData.roomId,
                    type: 'text',
                    voice: voice,
                    content: content,
                    uid: gameData.uid
                });
                network.wsData("Say/" + msg);
            } else if (gameData.subModule == SUB_MODULE.HuZi) {
                var msg = JSON.stringify({
                    roomid: gameData.roomId,
                    type: 'text',
                    voice: voice,
                    content: content,
                    uid: gameData.uid
                });
                network.sendPhz(5000, "Say/" + msg);
            }
        }
    }
});

